/**
 * 用地勘测接口服务
 */
import common from './common'
import { cloneDeep } from 'lodash'

/**
 * 获取勘测定界列表
 * @param projectId
 * @param resourceId
 * @returns {Promise | Promise<unknown>}
 */
export function getKcBaseList (projectId, resourceId) {
  return common.get('ytgz/ydkc/getKcBaseList', { projectId, resourceId })
}

/**
 * 新建勘测定界
 * @param projectId
 * @param id
 * @param name
 * @param bz
 * @returns {Promise | Promise<unknown>}
 */
export function createKcBase ({ projectId, id, name, bz }) {
  return common.post('ytgz/ydkc/createKcBase', {}, {
    projectId,
    resourceId: id,
    kcdjmc: name,
    bz
  })
}

/**
 * 更新勘测定界
 * @param id
 * @param kcdjmc
 * @param bz
 * @returns {Promise | Promise<unknown>}
 */
export function updateKcBase ({ id, kcdjmc, bz }) {
  return common.post('ytgz/ydkc/updateKcBase', {}, {
    id,
    kcdjmc,
    bz
  })
}

/**
 * 删除勘测定界
 * @param id
 * @returns {Promise | Promise<unknown>}
 */
export function delKcBase (id) {
  return common.post('ytgz/ydkc/deleteKcBaseById', {
    kcBaseId: id
  })
}

/**
 * 获取技术报告书数据
 * @param projectId
 * @param resourceId
 * @returns {Promise | Promise<unknown>}
 */
export function getReportBook (projectId, resourceId) {
  return common.get('ytgz/ydkc/getReportBook', { projectId, resourceId })
}

/**
 * 保存技术报告书
 * @param projectId
 * @param resourceId
 * @param data
 * @returns {Promise | Promise<unknown>}
 */
export function saveReportBook (projectId, resourceId, data) {
  const postData = Object.assign({ projectId, resourceId }, data)
  return common.post('ytgz/ydkc/saveReportBook', {}, postData)
}

/**
 * 获取技术说明
 * @param projectId
 * @param resourceId
 * @returns {Promise | Promise<unknown>}
 */
export function getTechnicalNote (projectId, resourceId) {
  return common.get('ytgz/ydkc/getTechnicalNote', { projectId, resourceId })
}

/**
 * 保存技术说明
 * @param projectId
 * @param resourceId
 * @param data
 * @returns {Promise | Promise<unknown>}
 */
export function saveTechnicalNote (projectId, resourceId, data) {
  const postData = Object.assign({ projectId, resourceId }, data)
  return common.post('ytgz/ydkc/saveTechnicalNote', {}, postData)
}

/**
 * 获取勘测定界表数据
 * @param projectId
 * @param resourceId
 * @returns {Promise | Promise<unknown>}
 */
export function getKcRange (projectId, resourceId) {
  return common.get('ytgz/ydkc/getKcRange', { projectId, resourceId })
}

/**
 * 保存勘测定界表数据
 * @param projectId
 * @param resourceId
 * @param data
 * @returns {Promise | Promise<unknown>}
 */
export function saveKcRange (projectId, resourceId, data) {
  const postData = Object.assign({ projectId, resourceId }, data)
  return common.post('ytgz/ydkc/saveKcRange', {}, postData)
}

/**
 * 增加建设用地地块
 * @param projectId
 * @param resourceId
 * @param data
 * @returns {Promise | Promise<unknown>}
 */
export function createKcLandBase (projectId, resourceId, data) {
  const postData = Object.assign({ projectId, resourceId }, data)
  return common.post('ytgz/ydkc/createKcLandBase', {}, postData)
}

/**
 * 更新建设用地地块
 * @param data
 * @returns {Promise | Promise<unknown>}
 */
export function updateKcLandBase (data) {
  return common.post('ytgz/ydkc/updateKcLandBase', {}, data)
}

/**
 * 删除建设用地地块
 * @param id
 * @returns {Promise | Promise<unknown>}
 */
export function deleteKcLandBase (id) {
  return common.post('ytgz/ydkc/deleteKcLandBase', { kcLandBaseId: id })
}

/**
 * 获取建设用地地块清单
 * @param projectId
 * @param resourceId
 * @param search
 * @param pageIndex
 * @param pageSize
 * @returns {Promise | Promise<unknown>}
 */
export function getKcLandBaseList (projectId, resourceId, search = '', pageIndex = 1, pageSize = 10) {
  return common.get('ytgz/ydkc/getKcLandBaseList', {
    projectId,
    resourceId,
    pageIndex,
    pageSize,
    landName: search
  })
}

/**
 * 获取土地分类面积列表
 * @param projectId
 * @param resourceId
 * @param kcLandBaseId
 * @returns {Promise<unknown>}
 */
export function getKeLandAreaData (projectId, resourceId, kcLandBaseId, countEnable) {
  return common.get('ytgz/ydkc/getKeLandAreaData', { projectId, resourceId, kcLandBaseId, countEnable })
}

/**
 * 保存土地分类面积
 * @param projectId
 * @param resourceId
 * @param baseId
 * @param mainInfo
 * @param details
 * @returns {Promise | Promise<unknown>}
 */
export function saveKeLandAreaData ({ projectId, resourceId, baseId, mainInfo, details }) {
  const baseInfo = { projectId, resourceId, baseId }
  const areaList = [Object.assign(mainInfo, baseInfo)]
  const areaDetailList = cloneDeep(details)
  areaDetailList.forEach(o => Object.assign(o, baseInfo))
  const postData = { areaList, areaDetailList }
  return common.post('ytgz/ydkc/saveKeLandAreaData', {}, postData)
}

/**
 * 删除土地分类面积记录
 * @param id
 * @returns {Promise | Promise<unknown>}
 */
export function delKeLandAreaData (id) {
  const removeAreaList = [{ id }]
  const postData = { removeAreaList }
  return common.post('ytgz/ydkc/saveKeLandAreaData', {}, postData)
}

/**
 * 获取界址点成果表数据
 * @param projectId
 * @param resourceId
 * @param kcLandBaseId
 * @returns {Promise<unknown>}
 */
export function getKcLandResult (projectId, resourceId, kcLandBaseId) {
  return common.get('ytgz/ydkc/getKcLandResult', { projectId, resourceId, kcLandBaseId })
}

/**
 * 保存界址点成果表数据
 * @param projectId
 * @param resourceId
 * @param baseId
 * @param data
 * @returns {Promise | Promise<unknown>}
 */
export function saveKcLandResult (projectId, resourceId, baseId, data) {
  const postData = Object.assign({ projectId, resourceId, baseId }, data)
  return common.post('ytgz/ydkc/saveKcLandResult', {}, postData)
}
/**
 * 获取建设用地地块坐标列表
 * @param projectId
 * @param resourceId
 * @param kcLandBaseId
 * @param pageIndex
 * @param pageSize
 * @returns {Promise<unknown>}
 */
export function getKcLandCoordinateList ({ projectId, resourceId, kcLandBaseId, pageIndex = 1, pageSize = 10 }) {
  return common.get('ytgz/ydkc/getKcLandCoordinateList', {
    projectId,
    resourceId,
    kcLandBaseId,
    pageIndex,
    pageSize
  })
}

/**
 * 保存建设用地地块坐标列表
 * @param projectId
 * @param resourceId
 * @param baseId
 * @param data
 * @returns {Promise | Promise<unknown>}
 */
export function saveKcLandCoordinateList (projectId, resourceId, baseId, data) {
  const postData = [Object.assign({ projectId, resourceId, baseId }, data)]
  return common.post('ytgz/ydkc/saveKcLandCoordinateList', {}, postData)
}

/**
 * 删除建设用地地块坐标记录
 * @param id
 * @returns {Promise | Promise<unknown>}
 */
export function deleteKcLandCoordinate (id) {
  return common.post('ytgz/ydkc/deleteKcLandCoordinate', { kcLandCoordinateId: id })
}

/**
 * 导入建设用地地块坐标
 * @param projectId
 * @param resourceId
 * @param file
 * @returns {Promise | Promise<unknown>}
 */
export function importKcLandCoordinateList (projectId, resourceId, file) {
  const formData = new FormData()
  formData.set('file', file)
  return common.post('ytgz/ydkc/importKcLandCoordinateList', { projectId, resourceId }, formData)
}

/**
 * 调用gis生成图形校验带拐点图
 * @param coords
 * @param picName
 * @returns {*}
 */
export function outPutProjectGeo (coords, picName) {
  return common.post(window.config.VUE_APP_GIS + 'dmap-web/ytgz_business/outPutProjectGeo', null, { coords: coords, imageHeigtht: 300, imageWidth: 300, picName: picName })
}

/**
 * 调用gis生成图形校验带拐点图
 * @param coords
 * @param picName
 * @returns {*}
 */
export function outPutGeoWithLabel (coords, picName) {
  return common.post(window.config.VUE_APP_GIS + 'dmap-web/ytgz_business/outPutGeoWithLabel', null, { coords: coords, imageHeigtht: 300, imageWidth: 300, picName: picName })
}

/**
 * 获取土地分类面积汇总
 * @param projectId
 * @param resourceId
 * @returns {Promise<unknown>}
 */
export function getKcLandAreaDataCount (projectId, resourceId, countEnable) {
  return common.get('ytgz/ydkc/getKcLandAreaDataCount', { projectId, resourceId, countEnable })
}

export function attributeQueryById (id, sql) {
  const gisUrl = window.config.VUE_APP_GIS
  return common.post('/taskDelay/request', {}, {
      url: `${gisUrl}dmap-web/query/attributeQueryById?id=${id}&sql=${sql}`,
      method: 'get',
      params: {}
    }
  )
}

export function getGisLayerList (businessId) {
  const gisUrl = window.config.VUE_APP_GIS
  return common.post('/taskDelay/request', {}, {
    url: `${gisUrl}dmap-web/businessConfig/getConfigByBusinessName?businessName=${businessId}&type=businessLayer`,
    method: 'get',
    params: {
    }
  })
}

export function deleteGisDk (rklayer, objectId) {
  const gisUrl = window.config.VUE_APP_GIS
  return common.post('/taskDelay/request', {}, {
    url: `${gisUrl}dmap-web/ytgz_business/deleteFromDTSP`,
    method: 'post',
    body: {
      keyField: 'objectid',
      rklayer: rklayer,
      deletedDK: [objectId]
    }
  })
}

export function deleteGisDks (rklayer, objectIds) {
  const gisUrl = window.config.VUE_APP_GIS
  return common.post('/taskDelay/request', {}, {
    url: `${gisUrl}dmap-web/ytgz_business/deleteFromDTSP`,
    method: 'post',
    body: {
      keyField: 'objectid',
      rklayer: rklayer,
      deletedDK: objectIds
    }
  })
}
